package net.sh.dpinfrastructure.db;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

/**
 * 
 * 查询语句
 * 
 * @version 1.0
 * @since JDK1.7
 * @author shaohui
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc. All rights reserved.
 * @date 2016年5月3日 下午12:35:38
 */
public class DPSearchStatement {
    
    private LinkedHashMap<DPCriteriaType, Map<String, ? extends Object>> criteria;
    
    /**
     * 是否全文检索，默认普通查询
     */
    private boolean fullTextSearch = false;
    
    /**
     * 是否记录插入时间
     */
    private boolean insertTime = false;
    
    /**
     * 是否记录更新时间
     */
    private boolean updateTime = false;
    
    public DPSearchStatement() {
        criteria = new LinkedHashMap<DPCriteriaType, Map<String, ? extends Object>>();
    }

    
    public DPSearchStatement eq(String field, Object value) {
        criteria.put(DPCriteriaType.EQ, ImmutableMap.of(field, value));
        return this;
    }
    
    
    public DPSearchStatement like(String field, String text) {
        criteria.put(DPCriteriaType.LIKE, ImmutableMap.of(field, text));
        return this;
    }

    public DPSearchStatement unlike(String field, String text) {
        criteria.put(DPCriteriaType.UNLIKE, ImmutableMap.of(field, text));
        return this;
    }
    
    public DPSearchStatement lte(String field, Object value) {
        criteria.put(DPCriteriaType.LTE, ImmutableMap.of(field, value));
        return this;
    }

    
    public DPSearchStatement gte(String field, Object value) {
        criteria.put(DPCriteriaType.GTE, ImmutableMap.of(field, value));
        return this;
    }

    
    public DPSearchStatement lt(String field, Object value) {
        criteria.put(DPCriteriaType.LT, ImmutableMap.of(field, value));
        return this;
    }

    
    public DPSearchStatement gt(String field, Object value) {
        criteria.put(DPCriteriaType.GT, ImmutableMap.of(field, value));
        return this;
    }

    
    public DPSearchStatement between(String field, Object smaller, Object bigger) {
        criteria.put(DPCriteriaType.BETWEEN, ImmutableMap.of(field, Lists.newArrayList(smaller, bigger)));
        return this;
    }

    
    public DPSearchStatement lbetween(String field, Object smaller, Object bigger) {
        criteria.put(DPCriteriaType.LBETWEEN, ImmutableMap.of(field, Lists.newArrayList(smaller, bigger)));
        return this;
    }

    
    public DPSearchStatement rbetween(String field, Object smaller, Object bigger) {
        criteria.put(DPCriteriaType.RBETWEEN, ImmutableMap.of(field, Lists.newArrayList(smaller, bigger)));
        return this;
    }

    
    public DPSearchStatement lrbetween(String field, Object smaller, Object bigger) {
        criteria.put(DPCriteriaType.LRBETWEEN, ImmutableMap.of(field, Lists.newArrayList(smaller, bigger)));
        return this;
    }

    
    public DPSearchStatement neq(String field, Object value) {
        criteria.put(DPCriteriaType.NEQ, ImmutableMap.of(field, value));
        return this;
    }

    
    public DPSearchStatement in(String field, Collection<Object> values) {
        criteria.put(DPCriteriaType.IN, ImmutableMap.of(field, values));
        return this;
    }

    
    public DPSearchStatement in(String field, Object... values) {
        criteria.put(DPCriteriaType.IN, ImmutableMap.of(field, values));
        return this;
    }

    
    public DPSearchStatement nin(String field, Collection<Object> values) {
        criteria.put(DPCriteriaType.NIN, ImmutableMap.of(field, values));
        return this;
    }

    
    public DPSearchStatement nin(String field, Object... values) {
        criteria.put(DPCriteriaType.NIN, ImmutableMap.of(field, values));
        return this;
    }

    
    public DPSearchStatement and(DPSearchStatement andObject) {
        criteria.put(DPCriteriaType.AND, ImmutableMap.of("", andObject));
        return this;
    }

    
    public DPSearchStatement or(DPSearchStatement orObject) {
        criteria.put(DPCriteriaType.OR, ImmutableMap.of("", orObject));
        return this;
    }

    
    public DPSearchStatement not(DPSearchStatement notObject) {
        criteria.put(DPCriteriaType.NOT, ImmutableMap.of("", notObject));
        return this;
    }

    public boolean isFullTextSearch() {
        return fullTextSearch;
    }

    public void setFullTextSearch(boolean fullTextSearch) {
        this.fullTextSearch = fullTextSearch;
    }

    public boolean isInsertTime() {
        return insertTime;
    }

    public void setInsertTime(boolean insertTime) {
        this.insertTime = insertTime;
    }
    
    public boolean isUpdateTime() {
        return updateTime;
    }
    
    public void setUpdateTime(boolean updateTime) {
        this.updateTime = updateTime;
    }

    public LinkedHashMap<DPCriteriaType, Map<String, ? extends Object>> toMap() {
        return criteria;
    }
}
